#include "task_main.h"
#include <stdio.h>
#include <stdarg.h>
#include "strutil.hpp"
#include "zh_iSwitch.h"
#include "adc_mcp3421.h"
#include "Ammeter.hpp"
#define LOG_INFO(TAG, str, ...) Queue_Printf(&htx1, str, ##__VA_ARGS__)
Ammeter ammeter;
uint32_t Key_Value = 0;
char keyChar = 0;

/* USER CODE BEGIN Header_TaskInit */
/**
 * @brief  Function implementing the InitTask thread.
 * @param  argument: Not used
 * @retval None
 */
/* USER CODE END Header_TaskInit */
void TaskInit(void const *argument)
{
    /* USER CODE BEGIN TaskInit */
    board_base_init(); // DWT, RTT, UART, TIM
	ammeter.setGear(Ammeter::GEAR_5A);
	ammeter.setRate(Ammeter::RATE_60SPS_14BIT);
    /* USER CODE END TaskInit */
}

/* USER CODE BEGIN Header_TaskInput */
/**
 * @brief  Function implementing the InputTask thread.
 * @param  argument: Not used
 * @retval None
 */
/* USER CODE END Header_TaskInput */
void TaskInput(void const *argument)
{
    /* USER CODE BEGIN TaskInput */
    // LOG_INFO(LOGTAG, "TaskInput\n");
    /* Infinite loop */
	uint32_t Key_Input = (GPIOA->IDR & Key1_Pin) >> 7; //Key1
	Key_Input |= (GPIOB->IDR & 0x07) << 1; //key2,3,4 低电平有效
	Key_Value = iSWx_Handler(Key_Input);

	if (Key_Value) (LOG_INFO(LOGTAG,"Key_Value=%X\n\r",Key_Value));
	if (Key_Value & 0x01)
	{
		ammeter.setGear(Ammeter::GEAR_5A);

	}

	if (Key_Value & 0x02)
	{
		ammeter.setGear(Ammeter::GEAR_1A);


	}
	if (Key_Value & 0x04)
	{
		ammeter.setGear(Ammeter::GEAR_200MA);


	}
	if (Key_Value & 0x08)
	{
		ammeter.setGear(Ammeter::GEAR_1000UA);

	}

	iSW_Clear(iSW_Get_Handle(iSW0), iSW_LONG);
	

    /* USER CODE END TaskInput */
}


/* USER CODE BEGIN Header_TaskOutput */
/**
 * @brief Function implementing the OutputTask thread.
 * @param argument: Not used
 * @retval None
 */
/* USER CODE END Header_TaskOutput */
void TaskOutput(void const *argument)
{
    /* USER CODE BEGIN TaskOutput */
    // LOG_INFO(LOGTAG, "TaskOutput\n");

    /* Infinite loop */
	static uint32_t cnt = 0;
	cnt++;
	if (cnt % (1000/240))
	{
		ammeter.dataReflash();
	}
	if (cnt % 500 == 0) //500ms
	{
		

		HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
		// AmmeterAutoSwitch();

	}
	
    /* USER CODE END TaskOutput */
}

/* USER CODE BEGIN Header_TaskDisplay */
/**
 * @brief Function implementing the DisplayTask thread.
 * @param argument: Not used
 * @retval None
 */
/* USER CODE END Header_TaskDisplay */
void TaskDisplay(void const *argument)
{
	// int cnt = 0;
    /* USER CODE BEGIN TaskDisplay */
    // LOG_INFO(LOGTAG, "TaskDisplay\n");
    /* Infinite loop */
    for (;;)
    {



        // osDelay(50);
    }
    /* USER CODE END TaskDisplay */
}

uint8_t ASU_Buf[10]; // 解析器的缓存,Split分割后的数据的引索存储到这里
StrUtil ASU("Ammeter:",',',";",ASU_Buf,sizeof(ASU_Buf),NULL,0);

void AmmeterCMD(uint8_t* data, int size)
{
	ASU.setSrcAddress(data, size);
	if (ASU.parse((char*)data) == 0) return;
	char* pstr = ASU.getStr(0);

	switch (*pstr)
	{
	case 'a':
		if (ASU.strCmp(0, "auto"))
		{
			if (ASU.strCmp(1, "on"))
			{
				ammeter.autoModeStart();
				Queue_Printf(&htx1, "Ammeter auto mod is on\n\r");
			}
			else if (ASU.strCmp(1, "off"))
			{
				ammeter.autoModeStop();
				Queue_Printf(&htx1, "Ammeter auto mod is off\n\r");
			}
		}
		break;
	case 's':
		if (ASU.strCmp(0, "stop"))
		{
			ammeter.printStop();
		}
		else if (ASU.strCmp(0, "start"))
		{
			ammeter.printStart();
		}
		else if (ASU.strCmp(0, "switch"))
		{
			if (ASU.strCmp(1, "R0.004"))//Ammeter:switch,Rxxx;
			{
				Queue_Printf(&htx1, "Ammeter switch to %s\n\r", ASU.getStr(1));
				ammeter.setGear(Ammeter::GEAR_5A);
			}
			else if (ASU.strCmp(1, "R0.02"))
			{
				Queue_Printf(&htx1, "Ammeter switch to %s\n\r", ASU.getStr(1));
				ammeter.setGear(Ammeter::GEAR_1A);
			}
			else if (ASU.strCmp(1, "R0.1"))
			{
				Queue_Printf(&htx1, "Ammeter switch to %s\n\r", ASU.getStr(1));
				ammeter.setGear(Ammeter::GEAR_200MA);
			}
			else if (ASU.strCmp(1, "R20"))
			{
				Queue_Printf(&htx1, "Ammeter switch to %s\n\r", ASU.getStr(1));
				ammeter.setGear(Ammeter::GEAR_1000UA);
			}
			else if (ASU.strCmp(1, "Off"))
			{
				Queue_Printf(&htx1, "Ammeter Off\n\r");
				ammeter.setGear(Ammeter::GEAR_Off);
			}
			else if (ASU.strCmp(1, "auto"))
			{
				Queue_Printf(&htx1, "Ammeter auto switch to %d\n\r", ammeter.setGearAuto());
			}
		}
		break;
	case 'r':
		if (ASU.strCmp(0, "rate"))
		{
			if (ASU.strCmp(1, "3.75"))
			{
				ammeter.setRate(Ammeter::RATE_3_75SPS_18BIT);
			}
			else if (ASU.strCmp(1, "15"))
			{
				ammeter.setRate(Ammeter::RATE_15SPS_16BIT);
			}
			else if (ASU.strCmp(1, "60"))
			{
				ammeter.setRate(Ammeter::RATE_60SPS_14BIT);
			}
			else if (ASU.strCmp(1, "240"))
			{
				ammeter.setRate(Ammeter::RATE_240SPS_12BIT);
			}
			Queue_Printf(&htx1, "Ammeter Rate is %d\n\r", ammeter.getRate()>>2);
		}
	default:
		break;
	}
}

#if 1
/* USER CODE BEGIN Header_TaskMsgHandle */
/**
 * @brief Function implementing the MsgHandlerTask thread.
 * @param argument: Not used
 * @retval None
 */
/* USER CODE END Header_TaskMsgHandle */
void TaskMsgHandle(void const *argument)
{
    /* USER CODE BEGIN Task_MsgHandle */
    uint8_t *pdata;
    uint32_t size;
    // LOG_INFO(LOGTAG, "TaskMsgHandle\n");
    /* Infinite loop */

	// 获取任务通知值并清空
	if (hrx1.rxCpltFlag)      /* 读到了数据 */
	{
		hrx1.rxCpltFlag = 0;
		pdata = UART_GetRxData(&hrx1);
		size = UART_GetRxSize(&hrx1);
		UARTx_Printn(&huart1, (uint8_t*)"[RX]", 4);
		UARTx_Printn(&huart1, pdata, size);
		UARTx_Printn(&huart1, (uint8_t*)"\n\r", 2);
		AmmeterCMD(pdata, size);
	}
	
    
    /* USER CODE END Task_MsgHandle */
}
#endif
